import React from 'react'
import SeckillCss from './index.module.scss'


class Seckill extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            year: "0",
            month: "0",
            day: "0",
            hour: "0",
            minute: "0",
            second: "0",
            timeDiff: "0",
            auto: null
        }
    }
    render() {
        return (
            <div className={SeckillCss.main}>
                {
                    this.state.day !=="0"?(<p className={SeckillCss.day}>{this.state.day}<span>天</span></p>):''
                }
                <p>{this.state.hour}</p>:
                <p>{this.state.minute}</p>:
                <p>{this.state.second}</p>
            </div>
        )
    }
    componentDidMount = (sart, end) => {
        let startTime = sart || "2020-09-01 00:00:00"
        let endTime = end || "2020-10-01 00:00:00"

        let startTimeStamp = parseInt(new Date(startTime).getTime() / 1000)
        let nowTimeStamp = parseInt(new Date().getTime() / 1000)
        let endTimeStamp = parseInt(new Date(endTime).getTime() / 1000)
        if (endTimeStamp > nowTimeStamp && endTimeStamp > startTimeStamp) {
            if (nowTimeStamp > startTimeStamp) {
                // console.log("活动开始")
                let difference = endTimeStamp - nowTimeStamp;
                this.auto = setInterval(() => {
                    difference -= 1;
                    this.setState({
                        day: parseInt(difference / 60 / 60 / 24 % 360) + "",
                        hour: this.getAppNum(parseInt(difference / 60 / 60 % 24), 2),
                        minute: this.getAppNum(parseInt(difference / 60 % 60), 2),
                        second: this.getAppNum(parseInt(difference % 60), 2)
                    }, () => {
                        // console.log(this.state.year)
                        // console.log(this.state.month)
                        // console.log(this.state.day)
                        // console.log(this.state.hour)
                        // console.log(this.state.minute)
                        // console.log(this.state.second)
                    })
                    if (difference <= 1) {
                        clearInterval(this.auto)
                    }
                }, 1000)

            } else {
                console.log("活动未开始")
            }
        } else {
            console.log("一个错误的活动")
        }

    }
    // 销毁前
    componentWillUnmount() {
        clearInterval(this.auto)
        // 卸载异步操作设置状态
        this.setState = (state, callback) => {
            return;
        }
    }
    //返回指定位数字符串不够0补充
    getAppNum = (num, digit) => {
        var newNum = "";
        var zero = "";
        var number = "" + num;
        if (digit >= number.length) {
            for (var i = 0; i < digit - number.length; i++) {
                zero = "0" + zero;
            }
            newNum = zero + number;
        }
        return newNum;
    }
}
export default Seckill;